
#ifndef __BMANX_BOMB_HEADER_FILE__
#define __BMANX_BOMB_HEADER_FILE__

#include "moving.h"

////////////////////////////////////////////////////////////////////////////////
class bomb : public moving
{
    static collisionbody _stcoll;
    
    uchar _flsize[4];

    ushort _detcounter;
    uchar _seqid;               ///< seq.id for trigger firing
    int8 _flame_len;            ///< flame length, negative if just burning
    uchar _ignitor;
    uchar _last_touch;          ///< man who touched the bomb as last

public:

    COMMAND_CLASS(bomb,"id:b")

    void state_save( frame_command_buffer& fcb ) const
    {
        MARK_OUT('b');
        moving::state_save(fcb);
        fcb.setb( _flsize, (uchar*)&_last_touch - (uchar*)_flsize + sizeof(_last_touch) );
        MARK_OUT('b'|0x80);
    }

    void state_load( frame_command_reader& fcr )
    {
        MARK_IN('b');
        moving::state_load(fcr);
        fcr.getb( _flsize, (uchar*)&_last_touch - (uchar*)_flsize + sizeof(_last_touch) );
        MARK_IN('b'|0x80);
    }


    bomb() : moving(false,&_stcoll)
    {
        _detcounter = 0;
        _seqid = 0;
        _speed = SPEED_BOMB;
    }

    rsp_tile* frame()
    {
        if( _detcounter && !(_flags & fNO_COUNTDOWN) && _xyzd0.z <= coord(0) )
            --_detcounter;
        return 0;
    }

    void burn()
    {
        clr_trigger();
        set_flame( -get_flame() );

        set_burning();

        stop();
    }

    void set_counter( ushort c )        { _detcounter = c; }
    ushort get_counter() const          { return _detcounter; }

    void set_trigger( uchar trigno )
    {
        _flags |= fTRIGGER | fNO_COUNTDOWN;
        _seqid = trigno;
    }
    void clr_trigger()                  { _flags &= ~(fTRIGGER|fNO_COUNTDOWN); }
    bool is_trigger() const             { return (_flags & fTRIGGER) != 0; }
    bool is_trigger_unfired() const     { return (_flags & (fTRIGGER|fNO_COUNTDOWN)) == (fTRIGGER|fNO_COUNTDOWN); }
    
    uchar get_seqid() const             { return _seqid; }

    void kick( moving& m )
    {
        set_direction( m.get_direction(), m.get_direction() );
        _last_touch = m.get_man();
    }


    ///reset detonation counter when hit by a flame
    void prepare_detonation( uchar ign, ushort latency )
    {
        if( _flame_len > 0 )
        {
            _detcounter = latency;
            _ignitor = ign;
            _flags &= ~fNO_COUNTDOWN;
        }
    }

    uchar get_ignitor() const           { return _ignitor; }


    void set_last_touch( uchar m )      { _last_touch = m; }
    uchar get_last_touch() const        { return _last_touch; }


    void set_flame( int8 fl )
    {
        if(fl>0)
        {
            _ignitor = get_unique_bombid();       //initially ignited by itself
            _last_touch = get_man();
        }

        _flame_len = fl;
    }
    short get_flame() const             { return _flame_len; }

    uchar* get_flsize_ptr()             { return _flsize; }

    static coord get_bomb_radius()      { return _stcoll.get_radius(); }

    ////////////////////////////////////////////////////////////////////////////////
/*
    void rsp_attach( rsp_s_connector& ctrl )
    {
        ctrl.view_add(this);
    }

    void rsp_detach( rsp_s_connector& ctrl )
    {
        ctrl.view_del(this);
    }
*/
    //RSP_CLASS_NAME("b",bomb)
    ////////////////////////////////////////////////////////////////////////////////
    
};


#endif //__BMANX_BOMB_HEADER_FILE__
